#!/usr/bin/python

from Tkinter import *
import tkFileDialog
import tkMessageBox
from IO import *

class NewMap:
	def __init__(self, parent):
		top = self.top = Toplevel(parent)

		Label(top, text="Create a new Map").pack()
		#You can just use options to label the scales, but it's not as pretty as using regular labels.
		#Label(top, text="Rows").pack()
		
		#~ self.horiz_scale = Scale(top, from_=0,to=50,orient=HORIZONTAL, label = "Rows")
		#~ self.horiz_scale.pack()
		self.horiz_label = Label(top, text="Height")
		self.horiz_spinner = Spinbox(top, width = 3,from_=1,to=50)
		self.horiz_spinner.focus_set()
		self.horiz_spinner.bind("<Return>",self.set_horiz_scale)
		self.horiz_label.pack()
		self.horiz_spinner.pack()
		
		#Label(top, text="Columns").pack()
		
		#~ self.vert_scale = Scale(top,from_=0,to=50, label = "Columns")
		#~ self.vert_scale.pack()
		self.vert_label = Label(top, text="Width")
		self.vert_spinner = Spinbox(top, width = 3,from_=1,to=50)
		self.vert_spinner.focus_set()
		self.vert_spinner.bind("<Return>",self.set_vert_scale)
		self.vert_label.pack()
		self.vert_spinner.pack()
	
		self.b = Button(top, text="OK", command=self.make)
		self.b.pack()
		self.top.bind("<Escape>", self.cancel)
		self.q = Button(top,text="Cancel",command=self.cancel)
		self.q.pack()
		
	def set_horiz_scale(self, event):
		self.horiz_scale.set(self.horiz_spinner.get())
	def set_vert_scale(self, event):
		self.vert_scale.set(self.vert_spinner.get())
		
		#~ self.top.grab_set() # Make sure this is the window being used
	def make(self):
		#This should work.
		self.rows = (u'grass',None)
		if (int(self.horiz_spinner.get()) and int(self.vert_spinner.get())) and int(self.horiz_spinner.get()) < 50 and int(self.vert_spinner.get()) < 50:
			self.columns = [self.rows]*int(self.horiz_spinner.get())
			self.maparray = [self.columns]*int(self.vert_spinner.get())
			self.tmpfile = open('swap','w')
			XML_Out(self.maparray,self.tmpfile)
			self.top.focus_set()
			self.top.destroy()
		else:
			tkMessageBox.showerror("Numeric Error","Map an invalid size. You may only create maps up to 50x50 in size")
		#So that was ugly. So what?
	
	def cancel(self,event=None):
		self.top.focus_set()
		self.top.destroy()
		raise NoMapCreated

if __name__ == "__main__":
	print "This is a helper class. You can import it, but it doesn't do anything by its lonesome."